/**
 * 用户留言校验
 * @authors yanwl
 * @date 2014-09-03
 */
$(function(){
	$('#btnSumbit').on('click',function(){
		checkData();
	})
})
function checkData() {
	var userName = $.trim($('#name').val()),
		userTel = $.trim($('#tel').val()),
		companyName = $.trim($('#company').val());
	
	if(userName == '' || userName == '姓名') {
		changeStyle('姓名不能为空!');
		return;
	}
	if(userTel == '' || userTel == '电话') {
		changeStyle('用户电话不能为空!');
		return;
	}
	if(companyName == '' || companyName == '公司名称') {
		changeStyle('公司名称不能为空!');
		return;
	}
	
	if(!isMobile(userTel)) {
		changeStyle('电话号码格式错误!');
		return;
	}else {
		$('#errorMsg').html('正在提交信息......');
		$('#msg').attr("style","color:green");
		$('#errorMsg').attr("style","display:block");
		
		//表单对象序列化
		var params = $('.mt20').serialize();
		//ajax请求，提交用户留言信息
		$.ajax({
			type : "post",
			url : "/common/usermessage",
			async:false,
			data : params,
			success: function(data) {
				if(data) {
					$('#errorMsg').html('发送成功，我们将尽快联系您');
				}else {
					$('#errorMsg').html('信息提交失败，请重新操作！');
					$('#msg').attr("style","color:#f00");
				}
			}
		})
	}
}

function isMobile(tel) {
	 var mobileReg = /^1[345678][0-9]{9}$/;
     return mobileReg.test(tel);
}

function changeStyle(param) {
	$('#errorMsg').html(param);
	$('#errorMsg').attr("style","display:block");
	$('#msg').attr("style","color:#f00");
}